package com.earthquake.monitor_system.utils.urlData;

import java.io.*;
import java.net.URL;
import java.text.ParseException;
import java.util.concurrent.Future;

import com.earthquake.monitor_system.domain.SeismicInformation;
import com.earthquake.monitor_system.service.impl.SeismicInformationImpl;
import com.earthquake.monitor_system.utils.dateToStamp.DateToStamp;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

/**
 * 程序功能：该程序用于从中国地震台网中心爬取近一年的地震数据
 * @author
 * @version 1.0
 * */
@Controller
public class URLdata {
    //通过注解引入配置
    @Resource(name = "defaultThreadPool")
    private ThreadPoolTaskExecutor executor;
    @Autowired
   private SeismicInformationImpl seismicInformation;
    public static URLdata urLdata;
    @PostConstruct
    public void init() {
        urLdata = this;
        urLdata.seismicInformation = this.seismicInformation;
        urLdata.executor = this.executor;
    }
    public  void getdata() throws Exception {
        long startTIme = System.currentTimeMillis();
        String str = "http://www.ceic.ac.cn/speedsearch?time=5&&page=";
//        SeismicInformationImpl seismicInformation = new SeismicInformationImpl();
        //添加字符输出缓冲流
//        BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"));
        //定义统一资源标识符
        URL url = null;
        //定义字符缓冲类
        StringBuffer sb = new StringBuffer();
        //定义字节输入流
        InputStream is = null;
        //定义字符输入流，可修改编码表
        InputStreamReader isr =null;
        //定义字符输入缓冲流
        BufferedReader bf =null;
        //定义文件类
        Document document = null;
        Elements element =null;
        url = new URL(str+""+1);
        is = url.openStream();
        isr = new InputStreamReader(is,"UTF-8");//使用"UTF-8"编码
        bf = new BufferedReader(isr);
        String line =null;
        while((line = bf.readLine())!= null){
            sb.append(line+"\n");  //数据写入缓冲流中
        }
        //使用Jsoup进行html的解析
        document = Jsoup.parse(sb.toString()) ;
        element = document.select("tr");//网页节点
        for(int i=1;i<element.size();i++){
            String data = element.get(i).text();
            String[] s = data.split(" ");
            SeismicInformation seismicInformation1 = new SeismicInformation();
            seismicInformation1.setEarthquake_magnitude(s[0]);
            seismicInformation1.setTime(s[1]+" "+s[2]);
            seismicInformation1.setLatitude(s[3]);
            seismicInformation1.setLongitude(s[4]);
            seismicInformation1.setDepth(s[5]);
            seismicInformation1.setLocation(s[6]);
            String s1 = DateToStamp.dateToStamp(s[1] + " " + s[2]);
            seismicInformation1.setTimestamp(s1);
            if(urLdata.seismicInformation.queryTimestampEq(s1)==null) {
                urLdata.seismicInformation.insert(seismicInformation1);
            }
//            bw.write(data);
//            bw.newLine(); //写换行
//            bw.flush();   //刷新
            //System.out.println();
        }
        Element ele = document.getElementById("pagenum");//网页节点
        //目的为了读取翻页网页的数量，即该数据在很多相似的网页上
        String string = ele.text().toString();
        String[] arraystr = string.split(":");//分割字符串
        Integer isign = new Integer(arraystr[2]);//字符变数组
        int intsign = isign.intValue();
        bf.close();
        sb.delete(0, sb.length());
        //利用上面读到的翻页数量，进行网页的循环读取
        for(int sign=2; sign<=intsign; sign++){
            url = new URL(str+""+sign);
            is = url.openStream();
            isr = new InputStreamReader(is,"UTF-8");
            bf = new BufferedReader(isr);
            line =null;
            while((line = bf.readLine())!= null){
//                System.out.println(line);
                sb.append(line+"\n");
            }
            document = Jsoup.parse(sb.toString()) ;
            element =document.select("tr");
            for(int i=1;i<element.size();i++){
                String data = element.get(i).text();
                String[] s = data.split(" ");
                SeismicInformation seismicInformation1 = new SeismicInformation();
                seismicInformation1.setEarthquake_magnitude(s[0]);
                seismicInformation1.setTime(s[1]+" "+s[2]);
                seismicInformation1.setLatitude(s[3]);
                seismicInformation1.setLongitude(s[4]);
                seismicInformation1.setDepth(s[5]);
                seismicInformation1.setLocation(s[6]);
                String s1 = DateToStamp.dateToStamp(s[1] + " " + s[2]);
                seismicInformation1.setTimestamp(s1);
                if(urLdata.seismicInformation.queryTimestampEq(s1)==null) {
                    urLdata.seismicInformation.insert(seismicInformation1);
                }
//                bw.write(element.get(i).text());
//                bw.newLine();
//                bw.flush();
            }
            bf.close();
            sb.delete(0, sb.length());

        }
//        bw.close();
        System.out.println("已全部加载完");
        long endTIme = System.currentTimeMillis();
        System.out.println((endTIme - startTIme));
    }

}

